package frontend.user.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import core.address.model.AddressType;
import core.address.model.BasicAddress;
import core.profile.model.BasicCredential;
import core.profile.model.BasicProfile;
import core.profile.model.BasicProfileAv;
import core.profile.model.SecurityQuestion;
import core.util.HibernateCoreUtil;


public class EditProfileDao {
	
	public static boolean saveAddress(BasicAddress ba)
	{
		 SessionFactory sf = HibernateCoreUtil.getSessionFactory();

	      Transaction t = null;

	try {

	            Session s = sf.openSession();

	           t = s.beginTransaction(); // start a new transaction

	           	s.saveOrUpdate(ba);	          

	           t.commit(); // commit transaction

	              return true;

	   } catch (Exception ex) {

	System.err.println("Error -->" + ex.getMessage());

	if (t != null)

	         t.rollback();

	        return false;

	}

	}
	
	public static boolean updateBasicCredential(BasicCredential bc) {

	    SessionFactory sf = HibernateCoreUtil.getSessionFactory();

	  Transaction t = null;

	try {

	        Session s = sf.openSession();

	       t = s.beginTransaction(); // start a new transaction
	       	   s.saveOrUpdate(bc);
	       t.commit(); // commit transaction

	          return true;

	} catch (Exception ex) {

	System.err.println("Error -->" + ex.getMessage());

	if (t != null)

	     t.rollback();

	    return false;

	}

	}
	
	public static boolean updateSecurityQuestion (SecurityQuestion sq) {

	    SessionFactory sf = HibernateCoreUtil.getSessionFactory();

	  Transaction t = null;

	try {

	        Session s = sf.openSession();

	       t = s.beginTransaction(); // start a new transaction
	       	   s.saveOrUpdate(sq);
	       t.commit(); // commit transaction

	          return true;

	} catch (Exception ex) {

	System.err.println("Error -->" + ex.getMessage());

	if (t != null)

	     t.rollback();

	    return false;

	}

	}
	
public static boolean editProfile(BasicProfile basicProfile) {

        SessionFactory sf = HibernateCoreUtil.getSessionFactory();

      Transaction t = null;

      try {

            Session s = sf.openSession();

           t = s.beginTransaction(); // start a new transaction
           	s.saveOrUpdate(basicProfile);
       
           t.commit(); // commit transaction

              return true;

      } catch (Exception ex) {

    	  System.err.println("Error -->" + ex.getMessage());

    	  if (t != null)

         t.rollback();

        return false;

      }

 }


}
